package com.ifreshwing;

import android.app.Activity;
import android.support.multidex.MultiDexApplication;

import com.ifreshwing.bean.ParkListBean;
import com.ifreshwing.socket.SocketConnect;
import com.ifreshwing.utils.Log;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by lz on 2017/4/27.
 */

public class ParkSysApplication extends MultiDexApplication {
    private static ParkSysApplication instance;
    private List<Activity> list_activity = new LinkedList<Activity>();

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        setList();
        SocketConnect.getInstance().initSocket();
    }

    public synchronized static ParkSysApplication getInstance() {
        if (null == instance) {
            instance = new ParkSysApplication();
        }
        return instance;
    }

    public List<ParkListBean> getList() {
        return alllist;
    }

    public void setList() {
        this.alllist = getData();
    }

    private List<ParkListBean> getData() {
        List<ParkListBean> list = new ArrayList<>();
        ParkListBean bean = new ParkListBean();
        bean.setStatus(1);
        bean.setIllegal(false);
        bean.setParking_num("001");
        bean.setOccupy_carNum("辽AER555");
        bean.setPhone_num("13516030504");
        bean.setRemaining_time("50分钟");
        list.add(bean);

        bean = new ParkListBean();
        bean.setStatus(2);
        bean.setParking_num("002");
        list.add(bean);

        bean = new ParkListBean();
        bean.setStatus(3);
        bean.setIllegal(true);

        bean.setParking_num("003");
        bean.setOccupy_carNum("辽AER555");
        bean.setPhone_num("13516030504");
        bean.setRemaining_time("-30分钟");
        bean.setStart_time("2017.05.01 14:01");
        bean.setEnd_time("2017.05.01 14:01");
        bean.setArrears(2);
        bean.setRegion("原因：超时停车");
        list.add(bean);

        bean = new ParkListBean();
        bean.setStatus(4);
        bean.setParking_num("004");
        bean.setIllegal(true);

        bean.setOccupy_carNum("辽AER555");
        bean.setPhone_num("13516030504");
        bean.setRemaining_time("120分钟");

        bean.setStart_time("2017.05.01 14:01");
        bean.setEnd_time("2017.05.01 14:01");
        bean.setArrears(30);
        bean.setRegion("原因：占用多个车位");
        list.add(bean);

        bean = new ParkListBean();
        bean.setStatus(5);
        bean.setParking_num("005");
        bean.setRegion("原因：占用多个车位");
        list.add(bean);
        bean.setIllegal(true);

        bean = new ParkListBean();
        bean.setStatus(6);
        bean.setParking_num("006");

        bean.setOccupy_carNum("辽AER555");
        bean.setPhone_num("13516030504");
        bean.setRemaining_time("100分钟");
        bean.setIllegal(true);

        bean.setStart_time("2017.05.01 14:01");
        bean.setEnd_time("2017.05.01 14:01");
        bean.setArrears(50);
        bean.setRegion("原因：违规停车有碍车辆通行");
        list.add(bean);

        bean = new ParkListBean();
        bean.setStatus(7);
        bean.setParking_num("007");
        bean.setOccupy_carNum("辽AER555");
        bean.setPhone_num("13516030504");
        bean.setRemaining_time("3分钟");
        bean.setIllegal(false);

        list.add(bean);

        bean = new ParkListBean();
        bean.setStatus(8);
        bean.setParking_num("008");
        bean.setOccupy_carNum("辽AER555");
        bean.setPhone_num("13516030504");
        bean.setRemaining_time("－－");
        bean.setStart_time("2017.04.07 08:30");
        bean.setIllegal(false);

        list.add(bean);


        bean = new ParkListBean();
        bean.setStatus(9);
        bean.setParking_num("009");
        bean.setOccupy_carNum("辽AER555");
        bean.setPhone_num("13516030504");
        bean.setRemaining_time("－－");
        bean.setStart_time("2017.04.07 08:30");
        bean.setTemporary_num("00001");
        bean.setIllegal(false);

        list.add(bean);

        bean = new ParkListBean();
        bean.setStatus(10);
        bean.setParking_num("010");
        bean.setOccupy_carNum("辽AER555");
        bean.setPhone_num("13516030504");
        bean.setRemaining_time("免费");
        bean.setIllegal(false);
        list.add(bean);

        return list;
    }

    List<ParkListBean> alllist = new ArrayList<>();

    public List<ParkListBean> getIllegal_lst() {
        illegal_lst = new ArrayList<>();
        for(ParkListBean b : getData()){
            if(b.isIllegal()){
                illegal_lst.add(b);
            }
        }
        return illegal_lst;
    }



    public List<ParkListBean> getExpire_lst() {
        expire_lst = new ArrayList<>();
        for(ParkListBean b : getData()){
            if(!b.isIllegal()){
                expire_lst.add(b);
            }
        }
        return expire_lst;
    }


    List<ParkListBean> illegal_lst ;
    List<ParkListBean> expire_lst ;


    public void addActivity(Activity activity) {
        list_activity.add(activity);
    }

    // 关闭指定activity
    public void finishActivity(Class... cls) {
        try {
            for (Class c : cls) {
                for (Activity act : list_activity) {
                    if (act.getClass() == c) {
                        act.finish();
                        Log.e("已删除的activity为：" + act);
                    }
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 退出应用
     */
    public void exitApp() {
        try {
            for (Activity activity : list_activity) {
                if (activity != null)
                    activity.finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 杀死该应用进程
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(0);
        }
    }

}
